package love.yuanmengrobot.qdkaoqin.business;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.NetworkOnMainThreadException;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import love.yuanmengrobot.qdkaoqin.R;
import love.yuanmengrobot.qdkaoqin.adapter.KaoqinAdapter;
import love.yuanmengrobot.qdkaoqin.entity.AttendanceDate;
import love.yuanmengrobot.qdkaoqin.util.TeacherConstants;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class StudentKaoqinActivity extends AppCompatActivity {
    private String CLAZZ_ID;
    private String CLAZZ_NAME;
    private String MEMBER_ID;
    private String attendanceNumber;

    private EditText editNumberText;


    private Button akbutton;
    private Button backbutton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_number_signin);
        Intent intent = getIntent();
        CLAZZ_ID = intent.getStringExtra(TeacherConstants.CLAZZ_ID);
        MEMBER_ID = intent.getStringExtra(TeacherConstants.MEMBERID);
        System.out.println("班级id：" + CLAZZ_ID);
        System.out.println("用户id*：" + MEMBER_ID);
        initView();
    }


    public void initView() {
        editNumberText = findViewById(R.id.ed_number);
        akbutton = findViewById(R.id.bu_ak);
        backbutton = findViewById(R.id.bu_back);
        backbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        akbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                attendanceNumber = editNumberText.getText().toString();
                if (attendanceNumber != null && attendanceNumber.length() > 0) {

                    System.out.println(attendanceNumber + "&&&&&" + MEMBER_ID);
                    //开始签到
                    String value="?key="+attendanceNumber+"&memberid="+MEMBER_ID;
                    post(value);

                } else {
                    final AlertDialog alertDialog1 = new AlertDialog.Builder(StudentKaoqinActivity.this)
                            .setTitle("警告！")//标题
                            .setMessage("请输入签到名！")//内容
                            .setIcon(R.mipmap.ic_warning)
                            .create();
                    alertDialog1.show();
                }
            }
        });
//        cancelbutton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                finish();
//            }
//        });

    }

    private okhttp3.Callback callback = new okhttp3.Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
//            Log.e(TAG, "Failed to connect server!");
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response)
                throws IOException {
            if (response.isSuccessful()) {
                final String body = response.body().string();
                System.out.println("6777776:" + body);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        if (body != null && body.contains("成功")) {
                            final AlertDialog alertDialog1 = new AlertDialog.Builder(StudentKaoqinActivity.this)
                                    .setTitle("提示")//标题
                                    .setMessage("签到成功！")//内容
                                    .setIcon(R.drawable.ic_baseline_check_24)
                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                            finish();
                                        }
                                    })
                                    .create();
                            alertDialog1.show();

//                            finish();
                        } else {
                            final AlertDialog alertDialog1 = new AlertDialog.Builder(StudentKaoqinActivity.this)
                                    .setTitle("提示")//标题
                                    .setMessage("签到失败！")//内容
                                    .setIcon(R.mipmap.ic_warning)
                                    .create();
                            alertDialog1.show();
                        }
//                        Gson gson = new Gson();
//                        Type jsonType =////////****
//                                new TypeToken<List<Clazz>>() {
//                                }.getType();
//                        List<Clazz> clazzListResponse =
//                                gson.fromJson(body, jsonType);
//                        for (Clazz cl : clazzListResponse) {
//                            clazzAdapter.add(cl);
//                        }
//                        clazzAdapter.notifyDataSetChanged();
                    }
                });
            } else {
            }
        }
    };

    private void post(String value) {//参数：key+memberid

        Request request = new Request.Builder()
                .url(TeacherConstants.STU_TEA_ATTEN_C_URL + value)
                .get().build();

        try {
            OkHttpClient client = new OkHttpClient();
            client.newCall(request).enqueue(callback);
        } catch (NetworkOnMainThreadException ex) {
            ex.printStackTrace();
        }
    }

}
